/**
  ******************************************************************************
  * @文件     beep.c
  * @作者     hero_cao
  * @版本     V1.0.0
  * @日期     2025/07/23
  * @概要     beep底层驱动
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include <string.h>
#include "./beep/beep.h"
#include "./board/bsp.h"
#include "./lcd/lcd.h"
#include "./text/text.h"


/* Private macros ------------------------------------------------------------*/
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))

/* Private variables ---------------------------------------------------------*/
typedef struct
{
    uint8_t music[300];
    uint8_t time[300];
    uint8_t length;
	char name[20];
} music_t;

uint16_t tone[] = {P0, L1, L2, L3, L4, L5, L6, L7, M1, M2, M3, M4, M5, M6, M7,  H1, H2, H3, H4, H5, H6, H7};
music_t songs[3];
	
uint8_t pause_play = 0;
uint8_t cur_num = 0;
uint8_t key_press;
uint16_t cur_tone;

/**
  * @brief  蜂鸣器发声1
  * @param  frq-频率(Hz)
  * @retval 无
  */
static void beep_sound(uint16_t frq)
{
	uint32_t time;
    if (frq != P0) {
		time = 500000 / ((uint32_t)frq);
		BSP_GpioSet(eBEEP);
		Delay_us(time);		// 根据音量调整高电平时间
		BSP_GpioReset(eBEEP);
		Delay_us(time);		// 根据音量调整低电平时间
	} else {
		Delay_us(1000);
	}
}


/**
  * @brief  歌曲初始化
  * @param  无
  * @retval 无
  */
void songs_init(void)
{
	uint8_t i,j;
	
	//红尘情歌
	uint8_t music_arr[] = {		//音调
		5,5,6,8,7,6,5,6,0,0,
		5,5,6,8,7,6,5,3,0,0,
		2,2,3,5,3,5,6,3,2,1,
		6,6,5,6,5,3,6,5,0,0,

		5,5,6,8,7,6,5,6,0,0,
		5,5,6,8,7,6,5,3,0,0,
		2,2,3,5,3,5,6,3,2,1,
		6,6,5,6,5,3,6,1,	

		0,8,9,10,10,9,8,10,9,8,6,
		0,6,8,9,9,8,6,9,8,6,5,
		0,2,3,5,5,3,5,5,6,8,7,6,
		6,10,9,9,8,6,5,6,8
	};	
	uint8_t time_arr[] = {		//时间
		2,4,2,2,2,2,2,8,4,4, 
		2,4,2,2,2,2,2,8,4,4, 
		2,4,2,4,2,2,4,2,2,8,
		2,4,2,2,2,2,2,8,4,4, 

		2,4,2,2,2,2,2,8,4,4, 
		2,4,2,2,2,2,2,8,4,4, 
		2,4,2,4,2,2,4,2,2,8,
		2,4,2,2,2,2,2,8,

		4,2,2,2,4,2,2,2,2,2,8,
		4,2,2,2,4,2,2,2,2,2,8,
		4,2,2,2,4,2,2,5,2,6,2,4,
		2,2,2,4,2,4,2,2,12
	};
	
	//荷塘月色
	uint8_t music_arr1[] = {
		8,12,6,5,6,
		8,8,9,10,
		9,9,8,9,9,12,
		12,10,10,9,10,
		8,8,6,5,12,
		10,9,10,9,8,
		9,9,8,9,9,10,
		9,8,6,9,8,
		8,8,6,5,6,
		8,8,9,10,
		9,9,8,9,9,12,
		12,10,10,9,10,
		8,8,8,6,5,12,
		10,9,10,9,8,
		9,9,8,9,9,10,
		9,8,6,9,8,
		10,12,12,12,12,
		13,12,10,9,8,
		13,15,13,10,9,8,6,
		9,9,10,10,9,
		10,12,12,12,12,
		13,12,10,9,8,
		6,8,6,5,9,10,
		8,0
	};

	uint8_t time_arr1[] = {
		2,4,2,4,4,
		4,2,2,8,
		2,4,2,4,2,2,
		2,2,2,2,8,
		2,4,2,4,4,
		2,2,2,2,8,
		2,4,2,2,4,2,
		2,2,2,2,8,
		2,4,2,4,4,
		2,4,2,8,
		2,4,2,4,2,2,
		2,2,2,2,8,
		2,2,2,2,4,4,
		2,2,2,2,8,
		2,4,2,2,4,2,
		2,2,2,2,8,
		2,4,2,4,4,
		2,2,2,2,8,
		2,2,2,2,2,2,2,2,
		4,2,2,2,6,
		2,4,2,4,4,
		2,2,2,2,8,
		2,2,2,2,4,4,
		12,4
	};

	// 沉默是金
	uint8_t music_arr2[] = {
		10,12,12,13,12,12,10,9,
		10,10,10,9,8,8,
		13,15,15,16,15,15,13,12,10,
		12,
		13,15,15,16,15,15,13,12,10,
		12,12,12,13,10,10,12,10,

		9,13,12,10,9,10,
		12,

		10,12,12,13,12,12,10,9,
		10,10,10,9,8,8,
		13,15,15,16,15,15,13,12,10,
		12,
		13,15,15,16,15,15,13,12,10,
		12,12,12,13,10,10,12,10,

		9,13,12,10,9,
		8,
		15,15,16,17,17,17,16,15,
		13,15,15,13,12,12,
		13,15,15,16,15,15,13,12,10,
		12,
		13,15,15,16,15,
		13,12,12,13,10,10,12,10,
		9,10,9,9,10,11,
		12,
		10,12,12,13,12,12,10,9,
		10,10,10,9,8,8,
		13,15,15,16,15,15,13,12,10,
		12,
		13,15,15,16,15,15,13,12,10,
		12,12,12,13,10,10,12,10,
		9,9,13,12,10,9,
		8
	};
	
	uint8_t time_arr2[]={
		2,2,2,1,1,4,1,3,
		2,2,2,1,1,8,
		2,2,2,1,1,4,2,2,2,
		16,
		2,2,2,1,1,4,2,2,2,
		2,2,2,1,1,4,2,2,

		6,2,4,2,2,2,
		16,

		2,2,2,1,1,4,1,3,
		2,2,2,1,1,8,
		2,2,2,1,1,4,2,2,2,
		16,
		2,2,2,1,1,4,2,2,2,
		2,2,2,1,1,4,2,2,

		6,2,4,2,2,
		16,
		4,2,1,1,3,1,1,3,
		2,2,2,1,1,8,
		2,2,2,1,1,4,2,2,2,
		16,
		6,2,2,2,4,
		2,2,2,1,1,4,2,2,
		6,2,4,2,2,2,
		16,
		2,2,2,1,1,4,1,3,
		2,2,2,1,1,8,
		2,2,2,1,1,4,2,2,2,
		16,
		2,2,2,1,1,4,2,2,2,
		2,2,2,1,1,4,2,2,
		2,4,2,2,4,2,
		16
	};
	strcpy(songs[0].name,"红尘情歌");
	strcpy(songs[1].name,"荷塘月色");
	strcpy(songs[2].name,"沉默是金");
	for(j = 0; j < 3; j++) {
		if (j == 0) {
			for (i = 0; i < ARRAY_SIZE(music_arr); i++) {
				songs[0].music[i] = music_arr[i];
				songs[0].time[i] = time_arr[i];
			}
			songs[0].length = ARRAY_SIZE(music_arr);
		} else if (j == 1) {
			songs[j].length = ARRAY_SIZE(music_arr1);
			for(i = 0; i<songs[j].length; i++)
			{
				songs[j].music[i] = music_arr1[i];
				songs[j].time[i] = time_arr1[i];
			}
		} else if (j == 2) {
			songs[j].length = ARRAY_SIZE(music_arr2);
			for(i = 0; i < songs[j].length; i++) {
				songs[j].music[i] = music_arr2[i];
				songs[j].time[i] = time_arr2[i];
			}
		}
	}
	key_press = 0;
}

/**
  * @brief  蜂鸣器音乐播放
  * @param  无
  * @retval 无
  */
void beep_play_music(void)
{
	uint16_t e;
	
	songs_init();
	
	// 播放歌曲部分
	for(cur_tone=0; cur_tone < songs[cur_num].length; cur_tone++) {
		if (cur_num == 0){
			text_show_string(30, 190, lcddev.width - 60, 16, "红尘情歌", 16, 0, BLUE);
			Delay_ms(300);
		}
		else if (cur_num == 1){
			text_show_string(30, 190, lcddev.width - 60, 16, "荷塘月色", 16, 0, BLUE);
			Delay_ms(300);
		}
		else if (cur_num == 2){
			text_show_string(30, 190, lcddev.width - 60, 16, "沉默是金", 16, 0, BLUE);
			Delay_ms(300);
		}
		lcd_show_num(170, 190, cur_num, 3, 16, BLUE);
		
		LED_OFF(eLED_1);
		LED_ON(eLED_2);
		
		for(e=0; e<(songs[cur_num].time[cur_tone])*tone[songs[cur_num].music[cur_tone]]/10; e++){
			// 暂停处理
			while(pause_play) {
				BSP_GpioToggle(eLED_1);
				LED_OFF(eLED_2);
				Delay_ms(300);
			}
			if (key_press) break;
			
			beep_sound(tone[songs[cur_num].music[cur_tone]]);		// 蜂鸣器发声
		}
		if (key_press) break;
	}
}

/**
* @brief  蜂鸣器开机提示
  * @param  无
  * @retval 无
  */
void beep_tip(void)
{
	uint8_t i;
	
	for(i=0; i < 250; i++ ) {
		beep_sound(tone[songs[cur_num].music[cur_tone]]);
	}
}
